建立一個郵件服務器
原理：postfix用来做smtp，dovecot做pop3  saslauthd用来做smtp验证。
大致配置过程如下：postfix和dovecot把帐号交给saslauthd，saslauthd把帐号交给pam，pam通过samba和winbind联系AD
环境：
windows: windows 2003 已建立dgalltop.com.cn域
     IP 10.136.74.64
     hostname dg-svr03.dgalltop.com.cn     
linux:  debian
     IP 10.136.74.3
     hostname mis-debian.dgalltop.com.cn
     
一、把linux加到AD域中
1、vim /etc/krb5.conf
  [libdefaults]
    default_realm = DGALLTOP.COM.CN
    dns_lookup_realm = true
    dns_lookup_kdc = true
  [realms]
    DGALLTOP.COM.CN = {
      kdc = 10.136.74.64:88
      default_domain = dgalltop.com.cn
      admin_server = 10.136.74.64:749
    }
  [domain_realm]
    .dgalltop.com.cn = DGALLTOP.COM.CN
    dgalltop.com.cn = DGALLTOP.COM.CN

2、vim /etc/samba/smb.conf
  [global]
    	idmap gid = 10000-20000
    	domain master = no
    	winbind use default domain = yes
    	template shell = /bin/bash
    	netbios name = mis-debian
    	winbind enum users = yes
    	idmap uid = 10000-20000
    	password server = 10.136.74.64
    	template homedir = /home/%D/%U
    	workgroup = DGALLTOP
    	os level = 20
    	winbind enum groups = yes
    	security = domain
    	preferred master = no
    	winbind separator = /
    	domain logons = no

  [homes]
    	browseable = no
    	comment = %U 's Home Directories
    	valid users = %U
    	writeable = yes
    	path = /home/%D/%U
    	preexec = /home/buildhome %D %U %G
	
	vim /home/buildhome
    umask 0077
    if [ ! -d /home/$1 ]; then
    mkdir /home/$1
    chmod -R 777 /home/$1
    fi

    if [ ! -d /home/$1/$2 ]; then
    mkdir /home/$1/$2
    chown $2 /home/$1/$2
    chgrp $3 /home/$1/$2
    fi
    
  chmod 777 /home/buildhome
	
3、vim /etc/nsswitch.conf
    修改如下部分：
  passwd:       files winbind
  shadow:       files winbind
  group:        files winbind

  重新启动smb和winbind
  /etc/init.d/samba restart
  /etc/init.d/winbind restart

4、把linux加入ad
  net rpc join -S dg-svr03.dgalltop.com.cn -U administrator 
  会提示要求输入域管理员密码，成功后返回。

  使用命令wbinfo -g
  wbinfo -u 看能否取到AD的用戶和組

二、配置postfix
  vim /etc/postfix/main.cf
    queue_directory = /var/spool/postfix
    command_directory = /usr/sbin
    daemon_directory = /usr/libexec/postfix
    mail_owner = postfix
    myhostname = mid-debian.dgalltop.com.cn
    mydomain = dgalltop.com.cn
    myorigin = $mydomain
    inet_interfaces = all
    mydestination = dgalltop.com.cn, debian.dgalltop.com.cn, localhost, localhost.localdomain, localhost
    smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
    biff = no
    unknown_local_recipient_reject_code = 550
    mynetworks_style = subnet
    mynetworks = 10.136.74.0/24,127.0.0.0/8
    relay_domains = $mydestination
    alias_maps = hash:/etc/aliases
    alias_database = hash:/etc/aliases
    mail_spool_directory = /var/mail/
    debug_peer_level = 2
    debugger_command =
    smtpd_sasl_auth_enable = yes
    smtpd_sasl_security_options = noanonymous
    smtpd_sasl_local_domain = $mydomain
    append_dot_mydomain = no
    smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
    home_mailbox =Maildir/
    command_directory = /usr/sbin
    debug_peer_level = 2
    mail_owner = postfix     

  rm -r /var/run/saslauthd/
  mkdir -p /var/spool/postfix/var/run/saslauthd
  ln -s /var/spool/postfix/var/run/saslauthd /var/run
  chgrp sasl /var/spool/postfix/var/run/saslauthd
  adduser postfix sasl

三、配置sasl驗證    
  vi /etc/postfix/sasl/smtp.conf，內容如下
    pwcheck_method: saslauthd
    log_level:2
    mech_list:PLAIN LOGIN

  vi /etc/default/saslauthd
    把 MECH=shadow 改成 MECH=pam
    重启saslauthd
    /etc/init.d/saslauthd restart

四、配置dovecot
  vi /etc/dovecot/dovecot.conf 
    protocols = imap pop3
    listen = *
    disable_plaintext_auth = no
    log_timestamp = "%Y-%m-%d %H:%M:%S "
    login_dir = /var/run/dovecot/login
    mail_location =maildir:~/Maildir    
    mail_access_groups = mail
    protocol imap {
      }
    protocol pop3 {
      pop3_uidl_format = %08Xu%08Xv
      }
    auth default {
      mechanisms = plain
      passdb pam {
        }
      userdb passwd {
        }
      user = root
      socket listen {
          client {
            mode = 0660
            user = postfix
            group = postfix
            }
      }
    }
    
  重新啟動dovecot
  /etc/init.d/dovecot restart

五、配置pam驗證
  分别修改/etc/pam.d下的common-auth common-account common-session
    
  vim /etc/pam.d/common-auth
    auth sufficient  pam_winbind.so krb5_auth Krb5_ccache_type=FILE
    auth  sufficient pam_unix.so use_first_pass nullok_secure
    auth  required pam_deny.so
    
  vim /etc/pam.d/common-account
    account  sufficient pam_winbind.so use_first_pass cached_login
    account  required pam_unix.so
    
  vim /etc/pam.d/common-session
    session  required  pam_unix.so
    session  required  pam_mkhomedir.so umask=0022 skel=/etc/skel

六、安装配置squirrelmail
  apt-get install squirrelmail
  squirrelmail-configure
  主菜单如下，主要配置第 2 项
  1.  Organization Preferences
  2.  Server Settings
  3.  Folder Defaults
  4.  General Options
  5.  Themes
  6.  Address Books
  7.  Message of the Day (MOTD)
  8.  Plugins
  9.  Database
  10. Languages
  
  第 2 项配置后如下：
  1.  Domain                 : dgalltop.com.cn
  2.  Invert Time            : false
  3.  Sendmail or SMTP       : SMTP
  A.  Update IMAP Settings   : 10.136.74.3:143 (other)
  B.  Update SMTP Settings   : 10.136.74.3:25
  
  若要让squirrelmail支持中文显示，配置第10项 Languages
  1.  Default Language       : zh_CN
  2.  Default Charset        : GB2312
  3.  Enable lossy encoding  : false
 
  重新启动 apache2 即可看到squirrelmail中文界面
  
＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝    
後補
將maildir目錄移出用戶的主目錄
修改/etc/postfix/main.cf
#home_mailbox = Maildir/
mail_spool_directory =/var/mail/

修改/etc/dovecot/dovecot.conf
mail_location= maildir:/var/mail/%u/

重啟 postfix dovecot

＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝
